package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.AdvposDto;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("ydd_advpos")
@Slf4j
public class AdvposController {

	@Autowired
	private AdvposService advposService;

	@Autowired
	private AdvimgService advimgService;

	/**
	 * 广告位列表-分页
	 *
	 * @param advposdesc
	 * @param advposname
	 * @param page
	 * @param limit
	 * @return
	 */
	@GetMapping("list")
	public ResultData list(String advposdesc, String advposname, Integer page, Integer limit) {
		log.info("advposdesc={} advposname={} page={}limit={}", advposdesc, advposname, page, limit);
		Page<Advpos> pageInfo = new Page(page, limit);
		Page<AdvposDto> pageDto = new Page<>();
		LambdaQueryWrapper<Advpos> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.like(StringUtils.isNotEmpty(advposname), Advpos::getAdvposname, advposname);
		queryWrapper.like(StringUtils.isNotEmpty(advposdesc), Advpos::getAdvposdesc, advposdesc);
		advposService.page(pageInfo, queryWrapper);
		BeanUtils.copyProperties(pageInfo,pageDto,"records");
		List<Advpos> pageInfoList = pageInfo.getRecords();
		List<AdvposDto> advposDtoList = pageInfoList.stream().map(item -> {
			AdvposDto advposDto = new AdvposDto();
			BeanUtils.copyProperties(item, advposDto);
			int id = item.getId();
			LambdaQueryWrapper<Advimg> advimgLambdaQueryWrapper = new LambdaQueryWrapper<>();
			advimgLambdaQueryWrapper.eq(Advimg::getAdvimgpos, id);
			int count = advimgService.count(advimgLambdaQueryWrapper);
			advposDto.setAdvposnum(count);
			return advposDto;
		}).collect(Collectors.toList());
		Long count = (long) advposService.count(queryWrapper);
		Long totalPage = count / limit;
		Long pageSize = Long.valueOf(limit);
		return ResultData.ok("",PageResult.init(count,pageSize,totalPage,advposDtoList));
	}

	/**
	 * 广告位列表-全部
	 * @return
	 */
	@GetMapping("all")
	public ResultData all(){
		List<Advpos> list = advposService.list();
		for (Advpos advpos : list) {
			System.out.println(advpos);
		}
		return ResultData.ok("",list);
	}

	/**
	 * 添加广告位
	 * @param advpos
	 * @return
	 */
	@PostMapping("add")
	public ResultData add(@RequestBody Advpos advpos){
		advposService.save(advpos);
		return ResultData.ok("","添加广告位成功");
	}

	/**
	 * 广告位详情
	 * @param id
	 * @return
	 */
	@GetMapping("rowInfo")
	public ResultData getInfo(Integer id){
		LambdaQueryWrapper<Advpos> queryWrapper = new LambdaQueryWrapper<>();
		Advpos advpos = advposService.getById(id);
		return ResultData.ok("",advpos);
	}

	/**
	 * 广告位删除
	 * @param id
	 * @return
	 */
	@GetMapping("del")
	public ResultData delete(Integer id){
		advposService.removeById(id);
		return ResultData.ok("","删除成功");
	}

	/**
	 * 广告位修改
	 * @param advpos
	 * @return
	 */
	@PostMapping("update")
	public ResultData update(@RequestBody Advpos advpos){
		advposService.updateById(advpos);
		return ResultData.ok("","修改成功");
	}
}
